﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TjZheng.Base;
using TjZheng.CMS.Filters;
using TjZheng.CMS.Process;
using TjZheng.Domain.DataEntity;
using CJAtech.BBS.Business;
using TjZheng.Controls;
using CJAtech.BBS.DataEntity;
using TjZheng.Framework;

namespace TjZheng.CMS.Areas.BBS.Controllers
{
    /// <summary>
    /// 敏感词管理
    /// </summary>
    [DbSetFilter]
    public class BadWordsController : CMSController
    {
        DbSetInfo objDbSetInfo
        {
            get
            {
                return ViewBag.DbSetInfo as DbSetInfo;
            }
        }

        BizBadWords objBizBadWords { get; set; }

        public BadWordsController()
        {
        }

        public ActionResult Index()
        {
            return View();
        }

        [QueryMsSqlFilter]
        [AuthorizeFilter(PowerType.CheckLogin)]
        public ActionResult ListJson(int page, int rows)
        {
            this.objBizBadWords = new BizBadWords(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            return Json(objBizBadWords.GetPageResult(page - 1, rows, QueryResult.Condition, QueryResult.Parameters));
        }
        public ActionResult EditPage(int? id)
        {
            this.objBizBadWords = new BizBadWords(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            BadWords objBadWords = null;
            if (id.HasValue)
            {
                objBadWords = objBizBadWords.GetRecord(id.Value);
            }
            else
            {
                objBadWords = new BadWords();
            }
            return View(objBadWords);
        }
        [ValidateInput(false)]
        public ActionResult Save(BadWords objProvide)
        {
            this.objBizBadWords = new BizBadWords(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            InvokeResult objInvokeResult = new InvokeResult();
            if (objProvide == null)
            {
                objInvokeResult.ResultCode = "ProvideNull";
                objInvokeResult.ResultMessage = "保存失败";
                return Json(objInvokeResult, "text/plain");
            }
            BadWords objTarget = null;
            if (objProvide.Id <= 0)
            {
                objTarget = new BadWords();
                Save(objTarget, objProvide);
                objBizBadWords.Add(objTarget);
            }
            else
            {
                objTarget = objBizBadWords.GetRecord(objProvide.Id);
                if (objTarget == null)
                {
                    objInvokeResult.ResultCode = "DataNull";
                    objInvokeResult.ResultMessage = "保存失败";
                    return Json(objInvokeResult, "text/plain");
                }
                Save(objTarget, objProvide);
                objBizBadWords.Update(objTarget);
            }

            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "保存成功";
            return Json(objInvokeResult, "text/plain");
        }

        public ActionResult Delete(string id)
        {
            this.objBizBadWords = new BizBadWords(objDbSetInfo.ConnectionString, objDbSetInfo.TableExtension);
            InvokeResult objInvokeResult = new InvokeResult();
            if (id.IsNull())
            {
                objInvokeResult.ResultCode = "IDNull";
                objInvokeResult.ResultMessage = "请先选择一条数据";
                return Json(objInvokeResult, "text/plain");
            }
            objBizBadWords.DeleteIDString(id);
            objInvokeResult.ResultCode = "Success";
            objInvokeResult.ResultMessage = "删除成功";
            return Json(objInvokeResult, "text/plain");
        }
        void Save(BadWords objTarget, BadWords objProvide)
        {
            objTarget.Word = objProvide.Word.CutWord(256);
        }

    }
}
